package com.bosc.modeling.service;

import com.bosc.modeling.dao.gen.CompetitionUserEntityMapper;
import com.bosc.modeling.dao.gen.EnrollEntityMapper;
import com.bosc.modeling.dao.gen.TeamEntityMapper;
import com.bosc.modeling.pojo.gen.*;
import net.bytebuddy.asm.Advice;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author yuanyj
 * @version 1.0
 * @date 2019/11/22 10:31
 * @description
 */
@Service
public class EnrollService {
    @Autowired
    EnrollEntityMapper enrollEntityMapper;
    @Autowired
    TeamEntityMapper teamEntityMapper;
    @Autowired
    CompetitionUserEntityMapper competitionUserEntityMapper;

    public boolean hasEnrolled(String competitionId,String userId){
        CompetitionUserEntityExample example = new CompetitionUserEntityExample();
        example.createCriteria().andCompetitionIdEqualTo(competitionId).andUserIdEqualTo(userId);
        List<CompetitionUserEntity> competitionUserEntities = competitionUserEntityMapper.selectByExample(example);
        if (competitionUserEntities != null && competitionUserEntities.size() > 0){
            return true;
        }
        return false;
    }
    public int insertTeam(TeamEntity team){
        return teamEntityMapper.insertSelective(team);
    }
    public int insertEnroll(EnrollEntity enroll){
        return enrollEntityMapper.insertSelective(enroll);
    }
    public int insertCompUser(CompetitionUserEntity competitionUserEntity){
        return competitionUserEntityMapper.insertSelective(competitionUserEntity);
    }
    public List<TeamEntity> getTeamByName(String teamName){
        TeamEntityExample example = new TeamEntityExample();
        example.createCriteria().andIsOwnerEqualTo((short)0).andTeamNameLike("%"+teamName+"%");
        return teamEntityMapper.selectByExample(example);
    }
    public boolean isExceedLimit(String teamId){
        TeamEntityExample example = new TeamEntityExample();
        example.createCriteria().andTeamIdEqualTo(teamId);
        long count = teamEntityMapper.countByExample(example);
        if (count == 3){
            return  true;
        }else {
            return false;
        }
    }
}
